package org.netuno.psamata.security;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.netuno.psamata.PsamataException;

public class SHA256 {
	public static byte[] crypt(String text) throws PsamataException {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			
			md.update(text.getBytes("UTF-8")); // Change this to "UTF-16" if needed
			
			return md.digest();
		} catch (Exception e) {
			throw new PsamataException(e);
		}
	}
	
	public static String cryptHex(String text) throws PsamataException {
		return new String(Hex.encodeHex(crypt(text)));
	}
	
	public static String cryptBase64(String text) throws PsamataException {
		return new String(Base64.encodeBase64(crypt(text)));
	}
}
